package org.jeecg.modules.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.biz.entity.*;
import org.jeecg.modules.biz.mapper.EventBurnMapper;
import org.jeecg.modules.biz.mapper.EventExchangeMapper;
import org.jeecg.modules.biz.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 *
 * 闪兑事件service接口实现
 *
 */
@Service
public class EventBurnServiceImpl extends ServiceImpl<EventBurnMapper, EventBurn> implements EventBurnService {

    @Autowired
    private EventBurnMapper burnMapper;
    @Autowired
    private BurnAmountService amountService;
    @Autowired
    private BurnIncomeService incomeService;
    @Autowired
    private BurnIncomeDetailService incomeDetailService;

    @Override
    public EventBurn getLatestOne() {
        return burnMapper.getLatestOne();
    }

    @Override
    public boolean saveAndUpdate(EventBurn eventBurn) {
        // 燃烧事件入库
        this.save(eventBurn);
        // 参与分配的额度
        BigDecimal rebate = new BigDecimal(eventBurn.getAmount()).multiply(new BigDecimal(0.5)).setScale(0,BigDecimal.ROUND_DOWN);

        // 获取燃烧池总额度
        Long totalBurnAmountInt = amountService.getTotalBurnAmount();
        if(totalBurnAmountInt != null){
            BigDecimal totalBurnAmount = new BigDecimal(totalBurnAmountInt);

            // 拉取所有受益人列表
            List<BurnAmount> burnAmounts = amountService.list();

            for (BurnAmount burnAmount : burnAmounts){
                // 更新所有受益人燃烧收益
                BigDecimal currentAmount = new BigDecimal(burnAmount.getAmount());
                BigDecimal amount = currentAmount.divide(totalBurnAmount).multiply(rebate).setScale(0,BigDecimal.ROUND_DOWN);
                incomeService.updateBalanceByAddress(amount.intValue(), burnAmount.getAddress());

                // 添加燃烧收益明细
                BurnIncomeDetail incomeDetail = new BurnIncomeDetail(burnAmount.getAddress(), amount.longValue(), eventBurn.getTimestamp());
                incomeDetailService.save(incomeDetail);
            }
        }

        // 更新发起者燃烧总额度
        BurnAmount currentBurnAmount = amountService.getOne(new QueryWrapper<BurnAmount>().eq("address", eventBurn.getCaller()));

        if(currentBurnAmount == null){
            // 1、第一次参与燃烧 初始化总额度数据和收益数据
            currentBurnAmount = new BurnAmount(eventBurn.getCaller(), eventBurn.getAmount(), eventBurn.getDate());
            amountService.save(currentBurnAmount);

            BurnIncome burnIncome = new BurnIncome(eventBurn.getCaller(), eventBurn.getDate());

            // 所有人总燃烧额度为null 说明未曾有人进行过燃烧 本次为系统第一次燃烧操作 本次燃烧收益属于当事人
            if(totalBurnAmountInt == null){
                burnIncome.setBalance(rebate.longValue());
            }

            incomeService.save(burnIncome);
        } else{
            // 2、有过燃烧记录，更新总额度数据
            currentBurnAmount.setAmount(currentBurnAmount.getAmount() + eventBurn.getAmount());
            amountService.updateById(currentBurnAmount);
        }

        return true;
    }

    @Override
    public Long totalAmount4Today() {
        return burnMapper.totalAmount4Today();
    }


    public static void main(String[] args) {
        BigDecimal rebate = new BigDecimal(3000000).multiply(new BigDecimal(0.5)).setScale(0,BigDecimal.ROUND_DOWN);
        BigDecimal totalBurnAmount = new BigDecimal(20000000);
        BigDecimal currentAmount = new BigDecimal(4000000);
        BigDecimal amount = currentAmount.divide(totalBurnAmount).multiply(rebate).add(currentAmount).setScale(0,BigDecimal.ROUND_DOWN);
        System.out.println(amount);
    }
}
